package com.turnengine.client.local.faction.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.faction.bean.IFaction;
import com.turnengine.client.local.faction.command.AddFaction;
import com.turnengine.client.local.faction.command.GetFactionById;
import com.turnengine.client.local.faction.command.GetFactionByName;
import com.turnengine.client.local.faction.command.GetFactions;
import com.turnengine.client.local.faction.command.IAddFaction;
import com.turnengine.client.local.faction.command.IGetFactionById;
import com.turnengine.client.local.faction.command.IGetFactionByName;
import com.turnengine.client.local.faction.command.IGetFactions;
import com.turnengine.client.local.faction.enums.FactionUnited;
import java.util.List;

/**
 * The Local Faction Command Set.
 */
public class LocalFactionCommandSet implements ILocalFactionCommandSet {

	@Override
	public IFaction addFaction(long loginId, int instanceId, String name, String password, FactionUnited united, int playerLimit) {
		IAddFaction command = new AddFaction(loginId, instanceId, name, password, united, playerLimit);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IFaction> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IFaction getFactionById(long loginId, int instanceId, int id) {
		IGetFactionById command = new GetFactionById(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IFaction> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IFaction getFactionByName(long loginId, int instanceId, String name) {
		IGetFactionByName command = new GetFactionByName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IFaction> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IFaction> getFactions(long loginId, int instanceId) {
		IGetFactions command = new GetFactions(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IFaction>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
